package work.vcloud.baseinfo.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;

import work.vcloud.frame.controller.BaseController;
import work.vcloud.frame.dao.OpenDao;
import work.vcloud.frame.vo.OutputVo;

import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

@RestController
@Scope("prototype")
public class BaseDataController extends BaseController {
	@Resource
	private OpenDao openDao;
	
	public OpenDao getOpenDao() {
		return openDao;
	}
	public void setOpenDap(OpenDao openDap) {
		this.openDao = openDao;
	}
	
	/**
	 * @Description 删除教材
	 */
	@RequestMapping("api/version.remove")
	public OutputVo removeVersion() {
		try {
			Map<String, Object> map = getParameterMap();

			JSONArray dataList = (JSONArray) JSON.parse(map.get("ids").toString());
			if(dataList.size() > 0) {
				for (int i = 0; i < dataList.size(); i++) {
					//教材id
					map.put("id", dataList.get(i));
					map.put("sqlMapId", "version.delete");
					openDao.update(map);
					
					map.put("fk_version", dataList.get(i));
					map.put("sqlMapId", "textbook.deleteByVersion");
					openDao.update(map);
				}
			}

			return output("");
		} catch (Exception e) {
			e.printStackTrace();
			return output(e);
		}
	}

	/**
	 * @Description 删除课本
	 */
	@RequestMapping("api/textbook.remove")
	public OutputVo removeTextbook() {
		try {
			Map<String, Object> map = getParameterMap();

			JSONArray dataList = (JSONArray) JSON.parse(map.get("ids").toString());
			if(dataList.size() > 0) {
				for (int i = 0; i < dataList.size(); i++) {
					//课本id
					map.put("id", dataList.get(i));
					map.put("sqlMapId", "textbook.delete");
					openDao.update(map);
				}
			}

			return output("");
		} catch (Exception e) {
			e.printStackTrace();
			return output(e);
		}
	}

	
}
